<?php

declare(strict_types=1);

namespace App\Repositories\RebateGift;


use App\Models\MainDB\RebateGift\RebatePool as Model;

use App\Traits\Singleton;

class RebatePoolRepository
{
    use Singleton;

    public function getDefaultPoolType(): string
    {
        return Model::DEFAULT_POOL;
    }

    public function getPublicPoolType(): string
    {
        return Model::PUBLIC_POOL;
    }

    public function pools(bool $lock = false)
    {
        $query = Model::query();
        if ($lock) {
            $query->lockForUpdate();
        }
        return $query->get();
    }


    public function get()
    {
        $query = Model::query();
        return $query->get();
    }

    public function one(array $filters, bool $fail = false)
    {
        $query = Model::query();
        $query->where($filters);
        return $fail ? $query->firstOrFail() : $query->first();
    }

    public function find(int $id)
    {
        return $this->one(['id' => $id], true);
    }

    public function create(array $data)
    {
        $model = new Model();
        $model->fill($data);
        $model->save();
        $model->refresh();
        return $model;
    }

    public function update(int $id, array $data)
    {
        $query = Model::query();
        $query->where('id', $id);
        return $query->update($data);
    }
}
